package com.pi.pro.xcmg.am.was.web.cust.controller;


import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.model.AbstractEntity;
import com.pi.framework.core.model.IEnabled;
import com.pi.framework.core.model.IdEntity;
import com.pi.framework.core.model.IdTreeEntity;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.ConvertUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.doc.DocumentActionDto;
import com.pi.wms.entity.*;
import com.pi.wms.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/outbound/so/ca")
public class CustApplySoController extends BaseCrudController<TwaDocSoHeader, Long> {

	@Autowired
	private SoService service;

    @Autowired
    private WasCustomWorkService wasCustomWorkService;
	

	@Override
	protected ICrudService<TwaDocSoHeader, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "so";
	}

	@Override
	protected TwaDocSoHeader newModel(HttpServletRequest request) {
		TwaDocSoHeader so=new TwaDocSoHeader();
		so.setType(WmsConstants.SO_TYPE_WORK_ORDER);
		if (StringUtils.isNullOrBlank(so.getCategory())){
			so.setCategory(XcmgAmConstants.WORK_SOURCE_PO);
		}
		//so.setConsigneeId(XcmgAmConstants.CUSTOMER_ID_XCMG_AM);
		so.setConsigneeCode(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
		so.setEditable(true);
		so.setEnabled(true);
		return so;
	}

    @PostMapping({"save", "save/{id}"})
    public String save(Model model, @Valid @ModelAttribute("m") TwaDocSoHeader m, BindingResult result, HttpServletRequest request, RedirectAttributes redirectAttributes) {
         super.save(model, m, result, request, redirectAttributes);
         return this.redirect("");
    }


	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		query.put("category", XcmgAmConstants.WORK_SOURCE_PO);
		super.prepareDatatableConditions(query, input);
	}

	@PostMapping("createwave")
	public String createWave(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request,
							 RedirectAttributes redirectAttributes) {
		model.addAttribute("params", getParams(request));

		String msg="";
		if ((ids==null) || (ids.length==0)){
			addSuccessMessage(redirectAttributes, "执行成功");
			return this.redirect("");
		}
		//
		DocumentActionDto actionDto=new DocumentActionDto();
		msg = this.wasCustomWorkService.addToWave(ids,  false, actionDto);
		if (!StringUtils.isNullOrBlank(msg)){
			addErrorMessage(redirectAttributes, "合并创建波次失败:"+msg);
		}else {
			addSuccessMessage(redirectAttributes, "执行成功");
		}
		return this.redirect("");
	}


	@PostMapping("addtowave")
	public String addToWave(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request,
							RedirectAttributes redirectAttributes) {
		model.addAttribute("params", getParams(request));

		String msg="";
		if ((ids==null) || (ids.length==0)){
			addSuccessMessage(redirectAttributes, "执行成功");
			return this.redirect("");
		}
		//
		DocumentActionDto actionDto=new DocumentActionDto();
		msg = this.wasCustomWorkService.addToWave(ids, true, actionDto);
		if (!StringUtils.isNullOrBlank(msg)){
			addErrorMessage(redirectAttributes, "创建波次失败:"+msg);
		}else {
			addSuccessMessage(redirectAttributes, "执行成功");
		}
		return this.redirect("");
	}

	protected String redirect(String relativeUrl) {
		if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
			relativeUrl = "/" + relativeUrl;
		}
		return "redirect:" + this.referUrl() + relativeUrl;
	}

	protected String root(String relativeUrl) {
		if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
			relativeUrl = "/" + relativeUrl;
		}
		return this.referUrl() + relativeUrl;
	}


	private String referUrl(){
		return "/outbound/so/";
	}

}
